package jp.wasabeef.sample;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;
import jp.wasabeef.richeditor.RichEditor;

public class MainActivity extends AppCompatActivity {

  private RichEditor mEditor;
  private TextView mPreview;
  private boolean isBold = false;
  private boolean isItalic = false;
  private boolean isSubscript = false;
  private boolean isSuperSubscript = false;
  private boolean isStrikethrough = false;
  private boolean isUnderline = false;
  private String alignType = "left";
  private String listState = "none";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mEditor = (RichEditor) findViewById(R.id.editor);
    mEditor.setEditorHeight(200);
    mEditor.setEditorFontSize(22);
    mEditor.setEditorFontColor(Color.RED);
    //mEditor.setEditorBackgroundColor(Color.BLUE);
    //mEditor.setBackgroundColor(Color.BLUE);
    //mEditor.setBackgroundResource(R.drawable.bg);
    mEditor.setPadding(10, 10, 10, 10);
    //mEditor.setBackground("https://raw.githubusercontent.com/wasabeef/art/master/chip.jpg");
    mEditor.setPlaceholder("Insert text here...");
    //mEditor.setInputEnabled(false);

    mPreview = (TextView) findViewById(R.id.preview);
    mEditor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() {
      @Override
      public void onTextChange(String text) {
        mPreview.setText(text);
      }
    });
    mEditor.setOnDecorationChangeListener(new RichEditor.OnDecorationStateListener() {
      @Override public void onStateChangeListener(String text, List<RichEditor.Type> types) {
        for (RichEditor.Type type:RichEditor.Type.values()) {
          setTypeEnable(type, types.contains(type));
        }
      }
    });

    findViewById(R.id.action_undo).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.undo();
      }
    });

    findViewById(R.id.action_redo).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.redo();
      }
    });

    findViewById(R.id.action_bold).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(isBold){
          mEditor.setBold(false);
          isBold = false;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_italic).setBackgroundResource(R.drawable.shape_plain_border);
          }
        } else {
          mEditor.setBold(true);
          isBold = true;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_italic).setBackgroundResource(R.drawable.shape_red_border);
          }
        }
      }
    });

    findViewById(R.id.action_italic).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(isItalic){
          mEditor.setItalic(false);
          isItalic = false;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_italic).setBackgroundResource(R.drawable.shape_plain_border);
          }
        } else {
          mEditor.setItalic(true);
          isItalic = true;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_italic).setBackgroundResource(R.drawable.shape_red_border);
          }
        }
      }
    });

    findViewById(R.id.action_subscript).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setSubscript();
        isSubscript = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          findViewById(R.id.action_subscript).setBackgroundResource(R.drawable.shape_red_border);
        }
      }
    });

    findViewById(R.id.action_superscript).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setSuperscript();
        isSuperSubscript = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          findViewById(R.id.action_superscript).setBackgroundResource(R.drawable.shape_red_border);
        }
      }
    });

    findViewById(R.id.action_strikethrough).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(isStrikethrough){
          mEditor.setStrikeThrough(false);
          isStrikethrough = false;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_strikethrough).setBackgroundResource(R.drawable.shape_plain_border);
          }
        } else {
          mEditor.setStrikeThrough(true);
          isStrikethrough = true;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_strikethrough).setBackgroundResource(R.drawable.shape_red_border);
          }
        }
      }
    });

    findViewById(R.id.action_underline).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(isUnderline){
          mEditor.setUnderline(false);
          isUnderline = false;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_underline).setBackgroundResource(R.drawable.shape_plain_border);
          }
        } else {
          mEditor.setUnderline(true);
          isUnderline = true;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_underline).setBackgroundResource(R.drawable.shape_red_border);
          }
        }
      }
    });

    findViewById(R.id.action_heading1).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setHeading(1);
      }
    });

    findViewById(R.id.action_heading2).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setHeading(2);
      }
    });

    findViewById(R.id.action_heading3).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setHeading(3);
      }
    });

    findViewById(R.id.action_heading4).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setHeading(4);
      }
    });

    findViewById(R.id.action_heading5).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setHeading(5);
      }
    });

    findViewById(R.id.action_heading6).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setHeading(6);
      }
    });

    findViewById(R.id.action_txt_color).setOnClickListener(new View.OnClickListener() {
      private boolean isChanged;

      @Override
      public void onClick(View v) {
        mEditor.setTextColor(isChanged ? Color.BLACK : Color.RED);
        isChanged = !isChanged;
      }
    });

    findViewById(R.id.action_bg_color).setOnClickListener(new View.OnClickListener() {
      private boolean isChanged;

      @Override
      public void onClick(View v) {
        mEditor.setTextBackgroundColor(isChanged ? Color.TRANSPARENT : Color.YELLOW);
        isChanged = !isChanged;
      }
    });

    findViewById(R.id.action_indent).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setIndent();
      }
    });

    findViewById(R.id.action_outdent).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setOutdent();
      }
    });

    findViewById(R.id.action_align_left).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!alignType.equals("left")){
          alignType = "left";
          mEditor.setAlignLeft();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_align_left).setBackgroundResource(R.drawable.shape_red_border);
          }
        }

      }
    });

    findViewById(R.id.action_align_center).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!alignType.equals("center")){
          alignType = "center";
          mEditor.setAlignCenter();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_align_center).setBackgroundResource(R.drawable.shape_red_border);
          }
        }
      }
    });

    findViewById(R.id.action_align_right).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setAlignRight();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          findViewById(R.id.action_align_right).setBackgroundResource(R.drawable.shape_red_border);
        }
      }
    });

    findViewById(R.id.action_blockquote).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.setBlockquote();
      }
    });

    findViewById(R.id.action_insert_bullets).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!listState.equals("bullet")){
          listState = "bullet";
          mEditor.setBullets();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_insert_bullets).setBackgroundResource(R.drawable.shape_red_border);
          }
        }

      }
    });

    findViewById(R.id.action_insert_numbers).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!listState.equals("number")){
          listState = "number";
          mEditor.setNumbers();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            findViewById(R.id.action_insert_numbers).setBackgroundResource(R.drawable.shape_red_border);
          }
        }

      }
    });

    findViewById(R.id.action_insert_image).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.insertImage("https://raw.githubusercontent.com/wasabeef/art/master/chip.jpg",
          "dachshund", 320);
      }
    });

    findViewById(R.id.action_insert_youtube).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.insertYoutubeVideo("https://www.youtube.com/embed/pS5peqApgUA");
      }
    });

    findViewById(R.id.action_insert_audio).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.insertAudio("https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3");
      }
    });

    findViewById(R.id.action_insert_video).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.insertVideo("https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_10MB.mp4", 360);
      }
    });

    findViewById(R.id.action_insert_link).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.insertLink("https://github.com/wasabeef", "wasabeef");
      }
    });
    findViewById(R.id.action_insert_checkbox).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mEditor.insertTodo();
      }
    });
  }

  void setTypeEnable(RichEditor.Type type,boolean enable){
    if(enable) {
      switch (type){
        case BOLD:
          isBold = true;
          findViewById(R.id.action_bold).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case ITALIC:
          isItalic = true;
          findViewById(R.id.action_italic).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case SUBSCRIPT:
          isSubscript = true;
          findViewById(R.id.action_subscript).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case SUPERSCRIPT:
          isSuperSubscript = true;
          findViewById(R.id.action_superscript).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case STRIKETHROUGH:
          isStrikethrough = true;
          findViewById(R.id.action_strikethrough).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case UNDERLINE:
          isUnderline = true;
          findViewById(R.id.action_underline).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case H1:
          findViewById(R.id.action_heading1).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case H2:
          findViewById(R.id.action_heading2).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case H3:
          findViewById(R.id.action_heading3).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case H4:
          findViewById(R.id.action_heading4).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case H5:
          findViewById(R.id.action_heading5).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case H6:
          findViewById(R.id.action_heading6).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case ORDEREDLIST:
          listState = "number";
          findViewById(R.id.action_insert_numbers).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case UNORDEREDLIST:
          listState = "bullet";
          findViewById(R.id.action_insert_bullets).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case JUSTIFYCENTER:
          alignType = "center";
          findViewById(R.id.action_align_center).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case JUSTIFYFULL:
          break;
        case JUSTIFYLEFT:
          alignType = "left";
          findViewById(R.id.action_align_left).setBackgroundResource(R.drawable.shape_red_border);
          break;
        case JUSTIFYRIGHT:
          alignType = "right";
          findViewById(R.id.action_align_right).setBackgroundResource(R.drawable.shape_red_border);
          break;
      }
    } else {
      switch (type){
        case BOLD:
          isBold = false;
          findViewById(R.id.action_bold).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case ITALIC:
          isItalic = false;
          findViewById(R.id.action_italic).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case SUBSCRIPT:
          isSubscript = false;
          findViewById(R.id.action_subscript).setBackgroundResource(R.drawable.shape_plain_border);
        case SUPERSCRIPT:
          isSuperSubscript = false;
          findViewById(R.id.action_superscript).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case STRIKETHROUGH:
          isStrikethrough = false;
          findViewById(R.id.action_strikethrough).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case UNDERLINE:
          isUnderline = false;
          findViewById(R.id.action_underline).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case H1:
          findViewById(R.id.action_heading1).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case H2:
          findViewById(R.id.action_heading2).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case H3:
          findViewById(R.id.action_heading3).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case H4:
          findViewById(R.id.action_heading4).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case H5:
          findViewById(R.id.action_heading5).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case H6:
          findViewById(R.id.action_heading6).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case ORDEREDLIST:
          findViewById(R.id.action_insert_numbers).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case UNORDEREDLIST:
          findViewById(R.id.action_insert_bullets).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case JUSTIFYCENTER:
          findViewById(R.id.action_align_center).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case JUSTIFYFULL:
          break;
        case JUSTIFYLEFT:
          findViewById(R.id.action_align_left).setBackgroundResource(R.drawable.shape_plain_border);
          break;
        case JUSTIFYRIGHT:
          findViewById(R.id.action_align_right).setBackgroundResource(R.drawable.shape_plain_border);
          break;
      }
    }

  }
}
